using System;
using System.Collections.Generic;
using System.Text;

using System.Xml;

using GSN.Config;

namespace GSN.Rules
{
    public class RulesConfigManager
    {                    
        static public IRule[] getAllRules()
        {
            IRule[] rules = { new AllowedTimeRule(), new AllowedSendersRule() };
            return rules;
            /*List<IRule> rules = new List<IRule>();
            rules.Add(new AllowedTimeRule());
            rules.Add(new AllowedSendersRule());
            return rules.ToArray();*/
        }

        static public IRule[] loadRules(XmlNodeList rulesNodes)
        {
            //XmlNodeList rulesNodes = doc.SelectNodes("GSN/Settings/Rules/Rule");
            if (rulesNodes == null || rulesNodes.Count == 0)
                return null;

            List<IRule> rules = new List<IRule>(rulesNodes.Count);

            foreach (XmlNode ruleNode in rulesNodes)
            {
                XmlNode typeNode = ruleNode.SelectSingleNode("Type");
                if (typeNode == null)
                    continue;

                IRule rule = (IRule)Activator.CreateInstance(Type.GetType("GSN.Rules." + typeNode.InnerText));
                rule.load(ruleNode);

                rules.Add(rule);
            }

            return rules.ToArray();            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="rules">rules to save</param>
        /// <param name="writer">where to save</param>
        static public void saveRules(IRule[] rules, XmlWriter writer)
        {
            if (rules == null)
                return;

            writer.WriteStartElement("Rules");

            foreach (IRule rule in rules)
            {
                rule.save(writer);
            }

            writer.WriteEndElement(); // rules

        }

    }
}
